package com.olt.controller.oltcenter.oltClassController;

import com.olt.domain.oltCenter.OltClass;
import com.olt.domain.user.OltUser;
import com.olt.result.ErrorEnum;
import com.olt.result.PageResult;
import com.olt.result.Result;
import com.olt.server.oltCenter.oltClass.OltClassServer;
import com.olt.server.oltCenter.oltClass.dto.CreatOltClassInput;
import com.olt.server.oltCenter.oltClass.dto.GetOltClassInput;
import com.olt.server.oltCenter.oltStudentServer.StudentServer;
import com.olt.server.oltCenter.oltStudentServer.dto.DeleteStudentInput;
import com.olt.server.oltCenter.oltStudentServer.dto.StudentOutput;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import util.PageUtil;

import java.util.UUID;


@RestController
public class OltClassController {
    @Autowired
    OltClassServer oltClassServer;
    @Autowired
    StudentServer studentServer;


    @PostMapping("api/oltClass/getMyCreateClass")
    public PageResult<OltClass> getMyCreateClass(@RequestBody GetOltClassInput input){
        OltUser user = (OltUser) SecurityUtils.getSubject().getPrincipal();
        input.setMe(user.getUserId());
        var res=new PageResult<OltClass>();
        PageUtil.setPageRes(res,oltClassServer.getOltClass(input));
        res.setError(ErrorEnum.Success_Http);
        return res;
    }
    @PostMapping("api/oltClass/saveClass")
    public Result<String> getMyCreateClass(@RequestBody CreatOltClassInput input){
        OltUser user = (OltUser) SecurityUtils.getSubject().getPrincipal();
        input.setCreateUser(user.getUserId());
        var res=new Result<String>();
        try {
            oltClassServer.saveOltClass(input, user.getUserId());
            res.setError(ErrorEnum.Success_Http);
        } catch (Exception e) {
            res.setError(ErrorEnum.MyError(e.getMessage()));
        }
        return res;
    }

    @GetMapping("api/oltClass/getStudentByClass")
    public  Result<StudentOutput> getStudentByClass(@RequestParam("id")String uuid){
        var res=studentServer.getStudentByClassId(uuid);
        var ret=new Result<StudentOutput>();
        ret.setError(ErrorEnum.Success_Http);
        ret.setList(res);
        return  ret;
    }
    @GetMapping("api/oltClass/createSessionKey")
    public Result<String> createSessionKey(@RequestParam("classId")String id){
        OltUser user = (OltUser) SecurityUtils.getSubject().getPrincipal();
        var res=new Result<String>();
        if(id==null){
            res.setError(ErrorEnum.MyError("无效的class"));
        }
        var key=oltClassServer.createSessionJoinKey(id,user.getUserId());
        if(key==null){
            res.setError(ErrorEnum.MyError("无权生成链接"));
        }
        else {
            res.setError(ErrorEnum.Success_Http);
            res.setValue(key);
        }
        return  res ;
    }

    @GetMapping("api/oltClass/sendJoinClassMessage")
    public Result<String> sendJoinClassMessage(@RequestParam("classId")String classId,@RequestParam("key")String key){
        OltUser user = (OltUser) SecurityUtils.getSubject().getPrincipal();
        var res=new Result<String>();
        try {
            res.setValue(oltClassServer.getJoinClass(classId,key, user.getUserId()));
            res.setError(ErrorEnum.Success_Http);
        }
        catch (Exception e){
            res.setError(ErrorEnum.MyError(e.getMessage()));
        }
        return res;
    }
    @GetMapping("api/oltClass/joinClass")
    public Result<String> joinClass(@RequestParam("classId")String classId,@RequestParam("key")String key){
        OltUser user = (OltUser) SecurityUtils.getSubject().getPrincipal();
        var res=new Result<String>();
        try {
            res.setValue(oltClassServer.joinClass(classId,key, user.getUserId()));
            res.setError(ErrorEnum.Success_Http);
        }
        catch (Exception e){
            res.setError(ErrorEnum.MyError(e.getMessage()));
        }
        return res;
    }

    @PostMapping("api/oltClass/deleteStudentFormClass")
    public Result<Boolean> deleteStudentFormClass(@RequestBody DeleteStudentInput input){
        var res=new Result<Boolean>();
        res.setError(ErrorEnum.Success_Http);
        res.setValue(oltClassServer.deleteStudentFormClass(input));
        return res;
    }
    @DeleteMapping("api/oltClass/deleteClass")
    public  Result<String> deleteRole(@RequestParam("ids") String[] ids){
        Result<String> res=new Result<>();
        oltClassServer.deleteClass(ids);
        res.setError(ErrorEnum.Success_Http);
        return res;
    }

    @GetMapping("api/oltClass/getClassForEdit")
    public Result<CreatOltClassInput> getClassForEdit(@RequestParam("classId")String classId){
        var res=new Result<CreatOltClassInput>();
        res.setValue(oltClassServer.getClassForEdit(classId));
        res.setError(ErrorEnum.Success_Http);
        return res;
    }

    @PostMapping("api/oltClass/getClassJoin")
    public PageResult<OltClass> getClassForEdit(@RequestBody GetOltClassInput input){
        OltUser user = (OltUser) SecurityUtils.getSubject().getPrincipal();
        var res=new PageResult<OltClass>();
        PageUtil.setPageRes(res,oltClassServer.getClassMyJoin(input, user.getUserId()));
        res.setError(ErrorEnum.Success_Http);
        return res;
    }


}
